package faircat.android.exphone;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

import android.os.Build;
import android.util.Log;

public class BluetoothSerialPort extends ATConnection {
	BluetoothAdapter mAdapter;
	BluetoothDevice mDevice;
	BluetoothSocket mSocket = null;
	BluetoothSocket mSocket1 = null;
	InputStream mInStream;
	OutputStream mOutStream;
	public static final int BT_SPP_PORT = 11;
	
	public BluetoothSerialPort(String address){
		mAdapter = BluetoothAdapter.getDefaultAdapter();
		mDevice = mAdapter.getRemoteDevice(address);
	}
	
	public BluetoothSerialPort(){
		mAdapter = BluetoothAdapter.getDefaultAdapter();
	}
	
	
	public void setDeviceWithAddress(String address) {
		mDevice =  mAdapter.getRemoteDevice(address);
	}

	public void close(){
		try{
			if( mSocket != null ){
				mSocket.close();
			}
			if( mSocket1 != null ){
				mSocket1.close();
			}
		}
		catch(Exception e){	
			Log.e("BluetoothSerialPort", "closed error");
		}
		Log.i("BluetoothSerialPort", "closed");
	}
	
	private boolean CONNECT_USE_REFLECT = false;
	public boolean connect() throws IOException {
		try{
			//Log.i("BluetoothSerialPort", "create Socket HandSet");
    		//mSocket1 = mDevice.createInsecureRfcommSocketToServiceRecord(BluetoothUUID.HandSet);
			//Log.i("BluetoothSerialPort", "connect HandSet");
    		//mSocket1.connect();  
			Log.i("BluetoothSerialPort", "create Socket SerialPort");			
			int sdk = Integer.parseInt(Build.VERSION.SDK);
			Log.i("BluetoothSerialPort", "sdk version:"+sdk);
			if (sdk >= 10) {
				if(!CONNECT_USE_REFLECT){
					mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(BluetoothUUID.SerialPort);
				}else{
					mSocket = BluetoothSocketFactory.createBluetoothSocket(
							BluetoothSocketFactory.TYPE_RFCOMM, 
							-1, 
							false, 
							false, 
							mDevice.getAddress(), 
							BT_SPP_PORT);
				}
			}else{
				if(!CONNECT_USE_REFLECT){
					mSocket = mDevice.createRfcommSocketToServiceRecord(BluetoothUUID.SerialPort);
				}else{
					mSocket = BluetoothSocketFactory.createBluetoothSocket(
							BluetoothSocketFactory.TYPE_RFCOMM, 
							-1, 
							false, 
							false, 
							mDevice.getAddress(), 
							BT_SPP_PORT);
				}
			}
			Log.i("BluetoothSerialPort", "create socket ok,use reflect:"+CONNECT_USE_REFLECT);
			if(mSocket != null){
				Log.i("BluetoothSerialPort", "connect SerialPort begin");
				mSocket.connect();
				Log.i("BluetoothSerialPort", "connect SerialPort done");
				mInStream = mSocket.getInputStream();
				mOutStream = mSocket.getOutputStream();
			}
		}
		catch(IOException e){
			Log.e("BLuetoothSerialPort", "connect error", e);
			CONNECT_USE_REFLECT = !CONNECT_USE_REFLECT;
			if(mSocket == null){
				mSocket.close();
				mSocket = null;
			}
			throw e;
		}
		
		return true;
	}
	
	public int read(byte[] buffer) throws IOException{
		try{
			return mInStream.read(buffer);
		}
		catch(IOException e){
			throw e;
		}
	}
	
	public boolean write(byte[] buffer) throws IOException{
		try{
			mOutStream.write(buffer);
		}
		catch(IOException e){
			throw e;
		}
		return true;
	}
	
	public int available() throws IOException{
		try{
			return mInStream.available();
		}
		catch(IOException e){
			throw e;
		}
	}
}
